import Taro from "@tarojs/taro";

export type IWindowInfo = {
    width: number;
    height: number;
    statusBarHeight: number;
    menuButtonRect: Taro.getMenuButtonBoundingClientRect.Rect;
}

export const getWindowInfo: IFnWithProps<
  Partial<{ windowInfo: IWindowInfo }>,
  IWindowInfo
> = () => {
  if (getWindowInfo.windowInfo) return getWindowInfo.windowInfo
  try {
    const res = Taro.getSystemInfoSync()
    const menuButtonRect = Taro.getMenuButtonBoundingClientRect()
    getWindowInfo.windowInfo = {
      width: res.windowWidth,
      height: res.windowHeight,
      statusBarHeight: res.statusBarHeight!,
      menuButtonRect
    }
    return getWindowInfo.windowInfo
  } catch (e) {
    console.log("[getSystemInfoSync] error: ", e)
    return getWindowInfo()
  }
}
